package com.dongnao.jack.invocate;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;

import com.alibaba.fastjson.JSONObject;
import com.dongnao.jack.invocate.bean.HttpInvocateParamBean;
import com.dongnao.jack.loadbalance.NodeRegisterInfo;
import com.dongnao.jack.service.store.ParseServiceFactory;
import com.dongnao.jack.spring.parse.ProtocolConfig;
import com.dongnao.jack.spring.parse.ReferenceConfig;
import com.dongnao.jack.spring.parse.RegistryConfig;
import com.dongnao.jack.util.HttpRequest;

/**
 * 
 * @ClassName: HttpRegistryInvocate 
 * @Description: 具体实现http协议的操作步骤
 * @author xuyp
 * @date 2018年4月19日 上午10:14:52 
 *
 */
public class HttpRegistryInvocate implements RegistryInvocate {

    private String contextPath = "/soadubboservice/httpservice/agent";

    @Override
    public String invoke(RegistryInvocation invocation) {

        // 获取到应用对象
        ReferenceConfig reference = invocation.getReference();

        NodeRegisterInfo registerInfo = invocation.getRegisterInfo();
        // JSONObject parseObject = JSONObject.parseObject(registerInfo.toString());
        ProtocolConfig protocol = ParseServiceFactory.getParseService(registerInfo.getType())
                .parseProtocol(registerInfo.getRegisterInfo().toString());

        try {

            String url = "http://" + protocol.getHost() + ":" + protocol.getPort() + contextPath;
            /*
             * 使用jsonobject
             * JSONObject paramJson = new JSONObject();
            paramJson.put("ref", ParseServiceFactory.getParseService(registerInfo.getType())
                    .parseRef(registerInfo.getRegisterInfo().toString()));
            paramJson.put("method", invocation.getMethod().getName());
            paramJson.put("args", invocation.getArgs());
            paramJson.put("paramTypes", invocation.getMethod().getParameterTypes());
            return HttpRequest.sendPost(url, paramJson.toJSONString());
            */

            // 使用封装bean
            HttpInvocateParamBean bean = new HttpInvocateParamBean();
            bean.setArgs(invocation.getArgs());
            bean.setMethodName(invocation.getMethod().getName());
            // bean.setParameterTypes(invocation.getMethod().getParameterTypes());
            bean.setRef(ParseServiceFactory.getParseService(registerInfo.getType())
                    .parseRef(registerInfo.getRegisterInfo().toString()));

            // return HttpUtils.post(url, map);
            return HttpRequest.sendPost(url, JSONObject.toJSONString(bean));
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    private RegistryConfig getRegistryConfig(String protocol, ApplicationContext context) {

        if (StringUtils.isBlank(protocol)) {
            return context.getBean(RegistryConfig.class);
        }

        Map<String, RegistryConfig> map = context.getBeansOfType(RegistryConfig.class);

        for (RegistryConfig config : map.values()) {
            if (config.getProtocol().equals(protocol)) {
                return config;
            }
        }

        return map.values().iterator().next();

    }

}
